package com.sh.weblogspringboot.common.domain.mapper;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sh.weblogspringboot.common.domain.dos.ArticleDO;
import com.sh.weblogspringboot.common.domain.dos.ArticlePublishCountDO;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;

import java.time.LocalDate;
import java.util.List;
import java.util.Objects;

/**
 * @author songHai
 * @Classname ArticleMapper
 * @Description Article Mapper
 * @Date 2025/4/8 23:01
 */
@Repository
public interface ArticleMapper extends BaseMapper<ArticleDO> {

    /**
     * 分页查询
     *
     * @param current   当前页码
     * @param size      每页展示的数据量
     * @param title     文章标题
     * @param startDate 开始时间
     * @param endDate   结束时间
     * @return
     */
    default Page<ArticleDO> selectPageList(Long current, Long size, String title, LocalDate startDate, LocalDate endDate) {
        // 分页对象
        Page<ArticleDO> page = new Page<>(current, size);

        // 构建查询条件
        LambdaQueryWrapper<ArticleDO> wrapper = Wrappers.<ArticleDO>lambdaQuery()
                .like(StringUtils.isNotBlank(title), ArticleDO::getTitle, title)
                .ge(Objects.nonNull(startDate), ArticleDO::getCreateTime, startDate)
                .le(Objects.nonNull(endDate), ArticleDO::getCreateTime, endDate)
                .orderByDesc(ArticleDO::getCreateTime);

        return selectPage(page, wrapper);
    }

    /**
     * 根据文章 ID 批量分页查询
     *
     * @param current
     * @param size
     * @param articleIds
     * @return
     */
    default Page<ArticleDO> selectPageListByArticleIds(Long current, Long size, List<Long> articleIds) {
        Page<ArticleDO> page = new Page<>(current, size);

        // 构建条件
        LambdaQueryWrapper<ArticleDO> wrapper = Wrappers.<ArticleDO>lambdaQuery()
                .in(ArticleDO::getId, articleIds)
                .orderByDesc(ArticleDO::getCreateTime);

        return selectPage(page, wrapper);
    }

    /**
     * 查询上一篇文章.
     *
     * @param articleId
     * @return
     */
    default ArticleDO selectPreByArticleId(Long articleId) {
        // 按文章 ID 升序排列, 查询比当前文章ID大的，第一条记录即为上一篇文章
        return selectOne(Wrappers.<ArticleDO>lambdaQuery()
                .gt(ArticleDO::getId, articleId)
                .orderByAsc(ArticleDO::getId)
                .last("limit 1"));
    }

    /**
     * 查询下一篇文章
     *
     * @param articleId
     * @return
     */
    default ArticleDO selectNextArticle(Long articleId) {
        // 按文章 ID 升序排列, 查询比当前文章ID小的，第一条记录即为下一篇文章
        return selectOne(Wrappers.<ArticleDO>lambdaQuery()
                .lt(ArticleDO::getId, articleId)
                .orderByDesc(ArticleDO::getId)
                .last("limit 1"));
    }

    /**
     * 阅读量+1
     *
     * @param articleId
     * @return
     */
    default int increaseReadNum(Long articleId) {
        // 执行SQL:  UPDATE t_article SET read_num =  read_num + 1 where (id=#{acticleId})
        return update(null, Wrappers.<ArticleDO>lambdaUpdate().setSql("read_num = read_num + 1").eq(ArticleDO::getId, articleId));
    }

    default List<ArticleDO> selectAllReadNum() {
        // 设置仅查询read_num字段
        return selectList(Wrappers.<ArticleDO>lambdaQuery().select(ArticleDO::getReadNum));
    }

    @Select("SELECT DATE(create_time) AS date, COUNT(*) AS count\n" +
            "FROM t_article\n" +
            "WHERE create_time >= #{startDate} AND create_time < #{endDate}\n" +
            "GROUP BY DATE(create_time)")
    List<ArticlePublishCountDO> selectDateArticlePubblishCount(LocalDate startDate, LocalDate endDate);
}
